package com.kltn.zigo.zingme.sdk;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.android.zing.ZME_Base;
import com.android.zing.ZME_Config;
import com.android.zing.ZME_DialogListener;
import com.android.zing.ZME_Environment;
import com.android.zing.api.ZME_Me;
import com.android.zing.api.ZME_Photo;
import com.android.zing.api.ZME_Social;
import com.android.zing.api.ZME_User;
import com.android.zing.api.ZME_ASyncGraphAPI.RequestListener;
import com.android.zing.lang.LanguageEnum;

public class ZingMeSDK {
	
	private static String TAG = "ZingMeSDK";
	
	private final String appName = "zigo";
	private final String appId = "e5e7fd50431a48a7b3206e13cdfd8207";
	private final String appSecret = "e4ad44ef86ce4a2f8435438b05cf1471";
	
private final boolean isFullScreen = true;
	
	private ZME_Config mConfig;
	private ZME_Base mZme;
    private ZME_Me mAsyncMe;
    private ZME_User mAsyncUser;
    private ZME_Photo mAsyncPhoto;
    private ZME_Social mAsyncSocial;   
    
	
	private final ZME_Environment env = ZME_Environment.PRODUCTION; 
	
	private static ZingMeSDK mInstance = null;
	
	public static ZingMeSDK getInstance() {
		if(mInstance == null) {
			mInstance = new ZingMeSDK();
		}
		return mInstance;
	}
	
	public ZingMeSDK() {		
		mConfig = new ZME_Config(appName, appId, appSecret, env, true, isFullScreen);
		mZme = new ZME_Base(mConfig);
		mAsyncMe = new ZME_Me(mZme);
        mAsyncUser = new ZME_User(mZme);
        mAsyncPhoto = new ZME_Photo(mZme);
        mAsyncSocial = new ZME_Social(mZme);
        mZme.setLanguage(LanguageEnum.EN);    
        
	}
	
	public ZME_Base getZME_Base() {
		return mZme;
	}
	
	public ZME_Me getZME_Me() {
		return mAsyncMe;
	}
	
	public ZME_User getZME_User() {
		return mAsyncUser;
	}
	
	public ZME_Photo getZME_Photo() {
		return mAsyncPhoto;
	}
	
	public ZME_Social getZME_Social() {
		return mAsyncSocial;
	}
	
	///////////////////// support multilang ////////////////
	public LanguageEnum getCurrentLang() {
		LanguageEnum ret = LanguageEnum.VI;
		try {
			ret = getZME_Base().getCurrentLang();
		}
		catch(Exception ex) {			
		}
		return ret;
	}
	
	public void setLang(LanguageEnum lang) {
		try {
			getZME_Base().setLanguage(lang);
		}
		catch(Exception ex) {			
		}
	}
		
	///////////////////// for authentication ///////////////
	/**
	 * Check to session valid or not
	 * @param ctx : Android
	 * @return
	 */
	public boolean isSessionValid(final Context ctx) {
		return mZme.isSessionValid(ctx);
	}
	
	/**
	 * Request login form to authenticate user login
	 * @param ctx : Android Application Context 
	 * @param state : a value provided by 3rd-party and it will be return back
	 * if success to prevent CSRF attacks
	 * @param listener
	 */
	public void authorize(final Activity activity, String state,
			final ZME_DialogListener listener) {
		mZme.authorize(activity, state, listener);
	}
	
	public void authorizeWithSSO(final Activity activity, String state,
			final ZME_DialogListener listener) {
		mZme.authorizeWithSSO(activity, state, listener);
//		mZme.authorize(activity, state, listener);
	}
	
	public void authorizeCallback(final Activity activity, int requestCode, int resultCode, Intent data) {
		mZme.authorizeCallback(activity, requestCode, resultCode, data);
	}
	
	public void logout(final Context ctx) {		
		mZme.logout(ctx);
	}
	
	/////////////////// for me.Info /////////////////////
	
	/** This function used to get Profile Info of user logged. 
	 * Data return in function onComplete is a JSONObject {"id":12345678,"tinyurl":"http:\/\/7.1.s50.avatar.zdn.vn\/avatar_files\/7\/8\/2\/0\/fortinet_50_56.jpg","username":"fortinet","displayname":"Nguyễn Minh Tú"} 
	 * 
	 * @param ctx : context of android application
	 * @param fields : Fields can be 'id','username','displayname','tinyurl','profile_url','gender','dob','ava160'.
	 * if fields empty, the return data include "id" and "username"
	 * @param listener : listener is a callback to parse data returned
	 * 
	 */
	public void getMeInfo(final Context ctx, String fields, final RequestListener listener) {
		try {
			getZME_Me().getInfo(ctx, fields, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in getMeInfo", ex);
		}
	}
	
	////////////////// for me.Friends //////////////////
	/***
	 * This function to get list friend id of user logged in.
	 * Data return in function onComplete is a JSONArray : [219862500,12100876,25916451,34572751,214674146,42471823]
	 * @param ctx : context of android application
	 * @param listener : listener is a callback to parse data returned
	 */
	public void getMeFriends(final Context ctx, final RequestListener listener ) {
		try {
			getZME_Me().getFriends(ctx, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in getMeFriends", ex);
		}		
	}
	
	/////////////// for me.isFanOf /////////////////////
	/***
	 * This function used to check user logged in whether is fan of fanpage on ZingMe or not.
	 * Data return in function onComplete is a JSONArray Of JSONObject : [{"id":82176178,"username":"munnguyen2804"},{"id":222399,"username":"minhnhat0809"},{"id":213312915,"username":"diengiadungso1"},{"id":213960142,"username":"nuest654321"},{"id":77829565,"username":"duyenchudien"}]
	 * @param ctx : context of android application
	 * @param zmeUsername : profile_name of fanpage
	 * @param listener : listener is a callback to parse data returned
	 */
	public void isFanOf(final Context ctx, String fanpage, final RequestListener listener ) {
		try {
			getZME_Me().isFanOf(ctx, fanpage, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in isFanOf", ex);
		}
	}
	
	///////////////// for user.Info ///////////////////
	/**
	 * This function used to get profile info of friends. 
	 * Remember maximum of uids to get info is 40, so if the friend list over 40, developer need to paging
	 * when get friend's profile info
	 * @param ctx : context of android application
	 * @param uids : list of uids need to get profile
	 * @param fields : Fields can be 'id','username','displayname','tinyurl','profile_url','gender','dob','ava160'.
	 * if fields empty, the return data include "id" and "username"
	 * @param listener : listener is a callback to parse data returned
	 */
	public void getUserInfo(final Context ctx, List<Integer> uids, String fields,
			final RequestListener listener) {
		try {
			getZME_User().getInfo(ctx, uids, fields, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in getUserInfo", ex);
		}
		
	}
	
	//////////////// for photo.Upload ////////////////
	/**
	 * 
	 * @param ctx : Android Application Context
	 * @param photoFile : Picture File upload
	 * @param description : description of picture to share 
	 * @param listener : listener is a callback to parse data returned
	 */
	public void uploadPhoto(final Context ctx, final File photoFile, String description, final RequestListener listener) {
		try {
			mAsyncPhoto.uploadPhoto(ctx, photoFile, description, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in uploadPhoto with file", ex);
		}
	}
	
	/***
	 * 
	 * @param ctx : Android Application Context
	 * @param filePath : Picture File upload path, extension must be jpeg, png.
	 * @param description : description of picture to share 
	 * @param listener : listener is a callback to parse data returned
	 * 
	 */
	public void uploadPhoto(final Context ctx, String filePath, String description, final RequestListener listener) {
		try {
			getZME_Photo().uploadPhoto(ctx, filePath, description, listener);	
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in uploadPhoto with filePath", ex);
		}		
	}

	/////////////// for social feature ///////////////
	/**
	 * This function normally is used to invite a friend on ZingMe to do some action app. 
	 * Friend will receive notification and feed on ZingMe
	 * Note : App must register full information (include logo 110x110, tittle, description) to ZingMe to make feed more nice
	 * such as logo 110x110, description.
	 * 
	 * @param ctx : Android Application Context
	 * 
	 * @param friendID : is userid of a friend want to invite, for example : 123456
	 * 
	 * @param link : linkUrl when do invite, for example https://play.google.com/store/apps/details?id=com.ulabs.thegioiphim
	 * 
	 * @param state : a variable provided by 3rd-party and it will be return back
	 * if success to prevent CSFR
	 * 
	 * @param  listener : listener is a callback to parse data returned
	 * 
	 * @return feed ID > 0 if success, return 0 if uidTo is not friend of user
	 *         logged with access_token or having error
	 *         
	 * @throws IOException
	 *             see ZME_Connection.requestJSON
	 * @throws ZingMeApiException
	 *             if ZingME return any error
	 * @throws ClientProtocolException
	 *             see ZME_Connection.requestJSON
	 * @throws NullPointerException
	 *             if response from server is null
	 */
	public void invite(final Context ctx, int friendID, String link, String state,
			final RequestListener listener) {
		try {
			getZME_Social().invite(ctx, friendID, link, state, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in invite", ex);
		}
	}
	
	/**
	 * This function is used to share an item of app/game, such as a book, a film, a song, an item or an achievement
	 * in game. Note : feed published by this function will have prefix "vừa chia sẻ ...." in title of feed  
	 *  
	 * @param ctx : Android Application Context
	 * 
	 * @param name : name of item share 
	 * 
	 * @param title : title of feed item
	 * 
	 * @param description : description of item
	 * 
	 * @param urlImg : image url of item (image has width maximum 110px)
	 * 
	 * @param titleLink : link of title
	 *  
	 * @param actionLink : link of action below of feed item
	 * 
	 * @param state : a value provided by 3rd-party and it will be return back
	 * if success to prevent CSRF attacks
	 *  
	 * @param listener : listener is a callback to parse data returned
	 * @throws IOException
	 *             see ZME_Connection.requestJSON
	 * @throws ZingMeApiException
	 *             if ZingME return any error
	 * @throws ClientProtocolException
	 *             see ZME_Connection.requestJSON
	 * @throws NullPointerException
	 *             if response from server is null
	 */
	public void share(final Context ctx, String name, String title, String description,
			String urlImg, String titleLink, String actionLink, String state,
			final RequestListener listener) {
		try {
			getZME_Social().share(ctx, name, title, description, urlImg, titleLink, actionLink, state, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in invite", ex);
		}
	}
	
	/**
	 * This function used to post a feed on ZingMe (such as used to post feed when a user upgrade new version of app/game)
	 * 
	 * @param ctx : Android Application Context
	 * 
	 * @param name : name of feed item  
	 *  
	 * @param title : title of feed item
	 * 
	 * @param description : description of feed item
	 * 
	 * @param urlImg : image url of item (image has width maximum 110px)
	 * 
	 * @param titleLink : link of title
	 * 
	 * @param actionLink : link of action below of feed item
	 * 
	 * @param state : a value provided by 3rd-party and it will be return back
	 * 
	 * @param useLargeImage : using large image template
	 * 
	 * if success to prevent CSRF attacks
	 * 
	 * @param listener : listener is a callback to parse data returned
	 */
	public void post(final Context ctx, String name, String title, String description,
			String urlImg, String titleLink, String actionLink, String state, boolean useLargeImageTemplate,
			final RequestListener listener) {
		try {
			getZME_Social().post(ctx, name, title, description, urlImg, titleLink, actionLink, state, useLargeImageTemplate, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in invite", ex);
		}
	}
	
	//////////////////////
	public void ir_onreceive(Context ctx, Intent intent) {
		try {
			getZME_Base().ir_onreceive(ctx, intent);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in ir_onreceive", ex);
		}
	}
	
	/***
	 * only call when application closed explicit
	 * @param context
	 */
	public void finalize(Context context) {
		try {
			ZME_Base.finalize(context);
		}
		catch(Exception ex) {
			
		}
	}
}